/*
 *   Copyright 1999-2018 dragonshard.net.
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.dragonshard.dsf.web.core.undertow;

import io.undertow.connector.ByteBufferPool;
import io.undertow.server.DefaultByteBufferPool;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
import java.io.IOException;
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.xnio.OptionMap;
import org.xnio.Options;
import org.xnio.Xnio;
import org.xnio.XnioWorker;

/**
 * 设置Undertow服务器 XnioWorker Buffers
 *
 * @author Caratacus
 * @see <a>https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-programmatic-embedded-container-customization</a>
 */
public class UndertowServerFactoryCustomizer implements
  WebServerFactoryCustomizer<UndertowServletWebServerFactory> {

  @Override
  public void customize(UndertowServletWebServerFactory factory) {
    UndertowDeploymentInfoCustomizer undertowDeploymentInfoCustomizer = deploymentInfo -> {
      WebSocketDeploymentInfo info = (WebSocketDeploymentInfo) deploymentInfo
        .getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
      XnioWorker worker = getXnioWorker();
      ByteBufferPool buffers = new DefaultByteBufferPool(
        Boolean.getBoolean("io.undertow.websockets.direct-buffers"), 1024, 100, 12);
      info.setWorker(worker);
      info.setBuffers(buffers);
    };
    factory.addDeploymentInfoCustomizers(undertowDeploymentInfoCustomizer);
  }

  /**
   * 获取XnioWorker
   */
  private XnioWorker getXnioWorker() {
    XnioWorker worker = null;
    try {
      worker = Xnio.getInstance().createWorker(OptionMap.create(Options.THREAD_DAEMON, true));
    } catch (IOException ignored) {
    }
    return worker;
  }

}
